package  
{
	/**
	 * just for test
	 * @author gray
	 */
	public class WF 
	{
		static public function wfIndex(rateArr:Array,random:Number=NaN):int
		{
			if(random > 1) throw new Error("random in [0,1]");
			if(!rateArr) throw new Error("rateArr is null");
			
			random = isNaN(random)?random:Math.random();
			
			var sum:Number = 0;
			var wf:Number = 0;
			var len:int = rateArr.length;
			
			for each(var rate:Number in rateArr)
			{
				sum += rate;
			}
			
			for (var i:int=0;i<len;i++)
			{
				wf += Number(rateArr[i]);
				if(random < wf) return i;
			}
			return 0;
		}
	}
}